package de.dopichaj.labrador.util.io;

import java.io.EOFException;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * Buffer for {@link RandomAccessFile}s that remembers the position in the file
 * and reads only up to a given mark.
 * 
 * Supports only readUnsignedByte.
 */
public final class BufferedDataInputFromRandomAccessFile implements ByteReader {
    
    private final RandomAccessFile file;
    private long filePos;
    private long endPos;
    
    private byte[] buffer = new byte[8192];
    private int bufferEnd = 0;
    private int bufferPos = 0;
    
    /**
     * Create a new one.
     * @param file
     *          The file to read from.
     * @param startPos
     *          The start position in the file.
     * @param endPos
     *          The logical end of file.
     */
    public BufferedDataInputFromRandomAccessFile(final RandomAccessFile file,
        final long startPos, final long endPos) {
    
        assert startPos <= endPos;
        this.file = file;
        this.filePos = startPos;
        this.endPos = endPos;
    }

    /* (non-Javadoc)
     * @see de.dopichaj.labrador.util.io.ByteReader#readUnsignedByte()
     */
    public int readUnsignedByte() throws IOException {
        if (bufferEnd == bufferPos) {
            if (atEOF()) {
                throw new EOFException("Reached logical EOF at " + filePos);
            }
            file.seek(filePos);
            bufferEnd = file.read(buffer, 0, (int)Math.min(endPos - filePos, buffer.length));
            if (bufferEnd == -1) {
                throw new EOFException();
            }
            assert bufferEnd > 0;
            filePos += bufferEnd;
            bufferPos = 0;
        }
        final byte result = buffer[bufferPos];
        bufferPos++;
        return result >= 0 ? result : result + 256;
    }

    /* (non-Javadoc)
     * @see de.dopichaj.labrador.util.io.ByteReader#atEOF()
     */
    public boolean atEOF() throws IOException {
        return bufferEnd == bufferPos && (filePos == endPos || filePos == file.length());
    }

}
/*
Copyright (c) 2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/